package com.lksy.core.aop;

import org.apache.commons.dbutils.QueryRunner;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import com.lksy.core.config.BaseDao;

/**
 * @author jzx
 * @date 2016-08-02 9:43:18 星期二
 */
@Component
@Aspect
public class ServiceAop extends BaseDao {

	@Pointcut("execution(* com.lksy.web.service..*(..))")
	public void makeOneCake() {
	}

	@Before("makeOneCake()")
	public void before(JoinPoint joinPoint) {
	}

	@After("makeOneCake()")
	public void after(JoinPoint joinPoint) {
	}

	@AfterReturning("makeOneCake()")
	public void afterReturn(JoinPoint joinPoint) {
	}

	@AfterThrowing(pointcut = "makeOneCake()", throwing = "ex")
	public void afterThrow(JoinPoint joinPoint, Exception ex) {
	}

	@Around(value = "makeOneCake()")
	public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
		String targetName = joinPoint.getTarget().getClass().getName();
		String methodName = joinPoint.getSignature().getName();
		long startTime = System.currentTimeMillis();
		Object object = joinPoint.proceed();
		long endTime = System.currentTimeMillis();
		QueryRunner run = new QueryRunner(dataSource);
		run.update("insert into method_log(method,time,clss) values(?,?,?)",methodName,(endTime - startTime),targetName);
		return object;
	}

}